

/******************************************************************************
* Copyright (C) 2021, BBI Technology Co.,Ltd All rights reserved.
*
* Author: Embedded software group
*
* file： api_valvemotor.c 
*
*
*Note：此文件位于对象层
 ******************************************************************************/
 
#include "app_infrared.h"

QueueHandle_t  infrared_Datarx_Queue = NULL;
SemaphoreHandle_t    infrared_BinarySemaphore=NULL;	//等到接收完成二值信号量句
void  APP_Mb_read_data(uint8_t *datbuf);
uint8_t infrared_data_handle(uint8_t *datbuf,uint8_t lenth);
void APP_infrared_ReadData(void)
{

    uint32_t  temp_bytes = 0; /*队列中字节长度new*/
	uint32_t cnt;
	static uint8_t  buff[50] = {0}; /*暂存接收协议,从0x68开始,用于crc计算*/
	static TickType_t StartTick = 0;
	static uint32_t currentBytes = 0;  /*old*/
    temp_bytes = uxQueueMessagesWaiting(infrared_Datarx_Queue);//检查消息数
	
	if(temp_bytes == 0)//检查队列的长度
	{
	   currentBytes = 0;
	}
	else
	{
		if(currentBytes != temp_bytes)//有新的数据
		{
			currentBytes = temp_bytes;
			StartTick = xTaskGetTickCount();
		}
		else
		{
			if(xTaskGetTickCount() - StartTick > 20) /*20ms,1帧结束*/
			{		
				for(cnt = 0; cnt<temp_bytes; cnt++)
				{
					xQueueReceive(infrared_Datarx_Queue,(void*)&buff[cnt],(TickType_t)10);//出队列,队列为空等待时间
				}	
				
	            infrared_data_handle(buff,temp_bytes);
			
				memset(buff,0,temp_bytes);
					
			}
		}
   }
}

uint8_t infrared_data_handle(uint8_t *datbuf,uint8_t lenth)
{
  uint16_t temp=usMBCRC16((uint8_t *)&datbuf[0],lenth-2 );
	
	if( temp==((uint16_t)datbuf[lenth-1]<<8)+datbuf[lenth-2] )
	{
	   APP_Mb_read_data(datbuf);
	}
	 return 0;
}


void  APP_Mb_read_data(uint8_t *datbuf)
{		
   	if(datbuf[4]==0 && datbuf[5]==1)
	{	
	   xSemaphoreGive( infrared_BinarySemaphore );	
	
	}		//写入的寄存器数量低
	
	xSemaphoreGive( infrared_BinarySemaphore );	
	
}

